package com.ruoyi.web.controller.insect.controller;


import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.insect.domain.DiseaseIdentify;
import com.ruoyi.insect.service.IDiseaseIdentifyService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 病虫害识别Controller
 *
 * @author ruoyi
 * @date 2023-08-07
 */
@RestController
@RequestMapping("/insect/identify")
public class DiseaseIdentifyController extends BaseController
{
    @Autowired
    private IDiseaseIdentifyService pestRecognitionService;

    /**
     * 识别病虫害
     */
    @PostMapping("/recognize")
    public AjaxResult recognize(@RequestParam("imageFile") MultipartFile file)
    {
        try {
            Map<String, Object> result = pestRecognitionService.recognizePest(file);
            return AjaxResult.success(result);
        } catch (Exception e) {
            return AjaxResult.error("识别失败: " + e.getMessage());
        }
    }



    /**
     * 查询病虫害识别记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:pestRecognition:list')")
    @GetMapping("/list")
    public TableDataInfo list(DiseaseIdentify pestRecognitionRecord)
    {
        startPage();
        List<DiseaseIdentify> list = pestRecognitionService.selectPestRecognitionRecordList(pestRecognitionRecord);
        return getDataTable(list);
    }

    /**
     * 导出病虫害识别记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:pestRecognition:export')")
    @Log(title = "病虫害识别记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DiseaseIdentify pestRecognitionRecord)
    {
        List<DiseaseIdentify> list = pestRecognitionService.selectPestRecognitionRecordList(pestRecognitionRecord);
        ExcelUtil<DiseaseIdentify> util = new ExcelUtil<DiseaseIdentify>(DiseaseIdentify.class);
        util.exportExcel(response, list, "病虫害识别记录数据");
    }

    /**
     * 获取病虫害识别记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:pestRecognition:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(pestRecognitionService.selectPestRecognitionRecordById(id));
    }

    /**
     * 新增病虫害识别记录
     */
    @PreAuthorize("@ss.hasPermi('system:pestRecognition:add')")
    @Log(title = "病虫害识别记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DiseaseIdentify pestRecognitionRecord)
    {
        return toAjax(pestRecognitionService.insertPestRecognitionRecord(pestRecognitionRecord));
    }

    /**
     * 修改病虫害识别记录
     */
    @PreAuthorize("@ss.hasPermi('system:pestRecognition:edit')")
    @Log(title = "病虫害识别记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DiseaseIdentify pestRecognitionRecord)
    {
        return toAjax(pestRecognitionService.updatePestRecognitionRecord(pestRecognitionRecord));
    }

    /**
     * 删除病虫害识别记录
     */
    @PreAuthorize("@ss.hasPermi('system:pestRecognition:remove')")
    @Log(title = "病虫害识别记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pestRecognitionService.deletePestRecognitionRecordByIds(ids));
    }
}